﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using CommonClass;
using System.Diagnostics;
using SurveillanceLib;
using CommonBase;


namespace MatrixManagement
{
    /// <summary>
    /// 大屏界面
    /// </summary>
    public partial class frm_AutoSplitWall : Form
    {
        #region 属性

        /// <summary>
        /// 获取所有已分割的窗口
        /// </summary>
        public List<Panel> PanelList { get; private set; }

        #endregion

        #region 变量

        PanelOperationExt panelOperationExt = new PanelOperationExt(); // 定义大屏界面分屏【简单分屏】，返回分屏列表
        Action<int> removeWallAction; // 销毁大屏
        ToolTip toolTip = new ToolTip(); // 添加 ToolTip

        #endregion

        #region 构造函数

        private frm_AutoSplitWall()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="removeWall">销毁大屏</param>
        public frm_AutoSplitWall(Action<int> removeWall)
            : this()
        {
            this.removeWallAction = removeWall;
        }

        #endregion

        #region 事件

        private void frm_AutoSplitWall_Load(object sender, EventArgs e)
        {
            panelTable.Left = panelTable.Top = 0;
            panelOperationExt.IsHideClickEvent = panelOperationExt.IsHideBorder = true; // 隐藏边框颜色、隐藏点击事件
        }

        private void frm_AutoSplitWall_FormClosed(object sender, FormClosedEventArgs e)
        {
             CloseForm();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close(); // 关闭窗口
        }

        #endregion

        #region 公开的方法

        /// <summary>
        /// 播放视频
        /// </summary>
        /// <param name="previewPanelList">预览模块中的视频列表</param>
        public void PlayForm(List<Panel> previewPanelList)
        {
            try
            {
                if (previewPanelList == null ||
                (previewPanelList != null && previewPanelList.Count <= 0))
                    return;

                if (this.Tag != null && this.Tag is TagModel &&
                    (this.Tag as TagModel).Wall_Index >= 0 &&
                    !(this.Tag as TagModel).Wall_IsShow) // 大屏未上墙时
                {
                    // 1. 分割上墙大屏
                    PanelList = panelOperationExt.SplitScreens(previewPanelList.Count, panelTable,
                        null, null, previewPanelList, false, toolTip);

                    // 2. 将 previewPanelList 的 Tag 值循环深拷贝存入 PanelList 中
                    for (int i = 0; i < previewPanelList.Count; i++)
                    {
                        if (previewPanelList[i] != null &&
                            previewPanelList[i].Tag != null &&
                            previewPanelList[i].Tag is TagModel &&
                            PanelList[i] != null)
                        {
                            (previewPanelList[i].Tag as TagModel).Panel_RealPlayHandle = string.Empty;
                            Utilities.CopyObject(previewPanelList[i].Tag, PanelList[i].Tag);
                            
                            TagModel panelTag = PanelList[i].Tag as TagModel;
                            int outChannel;

                            if (panelTag.Driver_Instance != null &&
                                !string.IsNullOrWhiteSpace(panelTag.Driver_LoginID) &&
                                !string.IsNullOrWhiteSpace(panelTag.Panel_SelectedChannel) &&
                                int.TryParse(panelTag.Panel_SelectedChannel, out outChannel))
                            {
                                panelTag.Panel_RealPlayHandle = panelTag.Driver_Instance.RealPlay(panelTag.Driver_LoginID,
                                    outChannel, PanelList[i].Handle); // c. 播放对应视频
                            }
                        }
                    }

                    // 3. 大屏上墙
                    TagModel wallTag = this.Tag as TagModel;
                    wallTag.Wall_IsShow = true; // 大屏已上墙
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        /// <summary>
        /// 关闭窗口
        /// </summary>
        public void CloseForm()
        {
            try
            {
                if (PanelList != null && PanelList.Count > 0 &&
                this.Tag != null && this.Tag is TagModel &&
                (this.Tag as TagModel).Wall_Index >= 0 &&
                (this.Tag as TagModel).Wall_IsShow) // 大屏已上墙时
                {
                    TagModel wallTag = this.Tag as TagModel;
                    wallTag.Wall_IsShow = false; // 大屏未上墙
                    // 1. 关闭所有视频
                    foreach (Panel wallSinglePanel in PanelList)
                    {
                        if (wallSinglePanel.Tag != null &&
                            wallSinglePanel.Tag is TagModel &&
                            (wallSinglePanel.Tag as TagModel).Driver_Instance != null &&
                            !string.IsNullOrWhiteSpace((wallSinglePanel.Tag as TagModel).Panel_RealPlayHandle))
                        {
                            TagModel tagSinglePanel = wallSinglePanel.Tag as TagModel;
                            if (tagSinglePanel.Driver_Instance.StopRealPlay(tagSinglePanel.Panel_RealPlayHandle)) // 停止实时监控，需要传递实时监控句柄
                                tagSinglePanel.Panel_RealPlayHandle = string.Empty;
                            else
                            {
                                MessageBox.Show(SurveillanceUtilities.STOP_REALPLAY_FAILURE, ConstantString.MSG_COMPANY);
                                return;
                            }
                        }
                    }

                    // 2. 当关闭大屏时，若显示该编辑详情界面，先播放对应的视频，再销毁主界面中对应大屏
                    if (this.removeWallAction != null)
                    {
                        this.removeWallAction(wallTag.Wall_Index);
                    }
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        #endregion
    }
}
